<!--
 * @Description: 关联设备列表
 * @Author: 0001100986/LiuShuai
 * @Date: 2021-11-02 16:50:46
 * @LastEditTime: 2021-11-02 19:16:58
 * @LastEditors: 0001100986/LiuShuai
-->
<template>
  <div class="relation-ne-box">
    <el-form :model="filterSearch" label-width="80px">
      <el-row>
        <el-col :xs="24" :sm="12" :lg="12">
          <el-form-item label="网元名称">
            <el-input v-model.trim="filterSearch.keyword" placeholder="请输入名称" />
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :lg="12">
          <el-form-item label-width="0" style="float: right;">
            <el-button type="primary" icon="el-icon-search" @click="search" style="padding: 5px;"></el-button>
            <el-button @click="reset" icon="el-icon-refresh" style="padding: 5px;"></el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-table
      class="table-box"
      :data="dataList"
      :showPage="true"
      ref="table"
      border
      height="250"
      @current-change="currentChange"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" align="center" width="55"></el-table-column>
      <el-table-column label="名称" min-width="200" show-overflow-tooltip prop="userlabel" />
      <el-table-column
        label="类型"
        align="center"
        width="100"
        sortable
        show-overflow-tooltip
        prop="typeLabel"
      />
      <el-table-column
        label="型号"
        align="center"
        width="100"
        sortable
        show-overflow-tooltip
        prop="subType"
      >
        <template slot-scope="scope">{{scope.row.subType | filterType }}</template>
      </el-table-column>
      <el-table-column
        label="租户"
        align="center"
        width="100"
        show-overflow-tooltip
        prop="extAttrs.dgName"
      />
      <el-table-column label="软件SN" align="center" width="180" prop="esn">
        <template slot-scope="scope">{{scope.row.extAttrs.esn | filterSN}}</template>
      </el-table-column>
      <el-table-column label="MAC地址" align="center" width="180" prop="mac">
        <template slot-scope="scope">{{scope.row.extAttrs.allMac}}</template>
      </el-table-column>
      <el-table-column label="IP" align="center" width="180" prop="ipAddress" />
      <el-table-column label="状态" align="center" width="100" prop="operResult">
        <template slot-scope="scope">
          <div v-show="scope.row.managementState === 'ONLINE'">
            <span v-if="scope.row.connectionState === 'CONNECTED'">
              <i class="i-success"></i>
              链接
            </span>
            <span v-if="scope.row.connectionState === 'DISCONNECTED'">
              <i class="i-error"></i>
              断链
            </span>
          </div>
          <span v-show="scope.row.connectionState === 'UNKNOWN'">
            <i class="i-warning"></i>
            未知
          </span>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :page-size="page.pageSize"
      :total="page.total"
      :page-sizes="[15]"
      :current-page.sync="page.page"
      @current-change="changePage"
      layout="total, sizes, prev, pager, next"
    ></el-pagination>
  </div>
</template>

<script>
export default {
  props: ['row'],
  data () {
    return {
      dataList: [],
      filterSearch: {
        keyword: ''
      },
      page: {
        page: 1,
        pageSize: 15,
        total: 0
      }
    };
  },
  mounted () {
    let _this = this;
    _this.$nextTick(() => {
      _this.refresh();
    });
  },
  methods: {
    // 搜索
    search () {
      let _this = this;
      _this.page.page = 1;
      _this.refresh();
    },
    // 点选
    currentChange (currentRow, oldCurrentRow) {
      let _this = this;
      _this.$refs.table.toggleRowSelection(currentRow);
      _this.$emit('handleSelNe', currentRow.id);
    },
    // 勾选项
    handleSelectionChange (val) {
      let _this = this;
      if (val.length > 1) {
        this.$refs.table.clearSelection();
        this.$refs.table.toggleRowSelection(val.pop());
      } else {
        _this.$emit('handleSelNe', val[0].id);
      }
    },
    // 重置
    reset () {
      let _this = this;
      _this.page.page = 1;
      _this.filterSearch = {
        keyword: ''
      };
      _this.refresh();
    },
    // 刷新
    refresh () {
      let _this = this;
      let params = {
        siteId: _this.row.id,
        keyword: _this.filterSearch.keyword,
        limit: _this.page.pageSize,
        offset: (_this.page.page - 1) * _this.page.pageSize
      };
      _this.$http.post({
        url: '/api/sdwan/site/siteNESelector',
        data: params
      }).then(res => {
        if (res.status === '0') {
          let { count, objects } = res.resultBody;
          _this.page.total = count;
          _this.dataList = objects;
        }
      });
    },
    // 切换页码 & 显示条数
    changePage (page) {
      if (page.page) {
        this.page.page = page.page;
      } else {
        this.page.page = 1;
        this.page.pageSize = page.limit;
      }
      this.refresh();
    }
  }
};
</script>

<style lang="scss" scoped>
.relation-ne-box {
  margin-bottom: 20px;
  ::v-deep thead th {
    background: rgb(248, 248, 249);
    color: rgb(102, 102, 102);
  }
  ::v-deep thead .el-table-column--selection .cell {
    display: none;
  }
}
</style>